/**
 *@auther: tang
 @Date: 2020-11-11
 *Project: group-booking-shop-manage-system
 */
import React from 'react'
import {inject,observer} from 'mobx-react'
import {  Menu } from 'antd';
import * as antIcons from '@ant-design/icons';
import getOneConsArr from "../util/oneConsArr";
import {Link} from 'react-router-dom'
import "./leftMenu.less"
const { SubMenu } = Menu;

@inject("userStore")
@observer
class LeftMenu extends
    React.Component
{
    constructor(props){
        super(props);
        this.state={
            leftMenu:[],
            defaultKey:"",
            defaultSelKey:"1",
            defaultSubKey:"1"
        }
    }
    bindMenu(menulist){
        let MenuList= menulist.map((item,index)=>{
            if (item.default){
                this.setState({
                    defaultKey:item.menuId
                })
            }
            if(item.menuChilds.length===0){  //没有  子菜单
                let IconMenu =antIcons[item.menuImgClass];
                return <Menu.Item key={item.menuId} icon={  <IconMenu />}>
                    <Link to={item.menuUrl}>{item.menuName}</Link>
                </Menu.Item>
            }
            else{
                let IconMenu =antIcons[item.menuImgClass];
                return <SubMenu key={item.menuId} icon={  <IconMenu />} title={item.menuName}>
                    {this.bindMenu(item.menuChilds)}
                </SubMenu>
            }
        })
        return MenuList;
    }
    componentDidMount(){
        let menuList =this.bindMenu(this.props.userStore.menuInfo);
        this.setState({
            leftMenu :menuList
        })
    }
    UNSAFE_componentWillMount() {
        //查找当前的item菜单
        console.log(this.props.father);
        let current = this.props.father.history.location.pathname;
        let menuInfo = this.props.userStore.menuInfo;
        let target = getOneConsArr(menuInfo).find(item=>{
            return item.menuUrl===current
        })
        //查找当前的sub菜单
        let subTarget = "";
        if (target){
            subTarget = menuInfo.find(item=>{
                if (item.menuChilds.length>0){
                    return item.menuChilds.find(mItem=>{
                        return mItem.menuId===target.menuId
                    })
                }
                else{
                    return ""
                }
            })
        }

        if (target&&current && subTarget){
            this.setState({
                defaultSelKey: target.menuId,
                defaultSubKey:subTarget.menuId
            })
        }

    }
    render()
    {
        return (
            <Menu

                mode="inline"
                 defaultSelectedKeys={[`${this.state.defaultSelKey}`]}
                 defaultOpenKeys={[`${this.state.defaultSubKey}`]}
                style={{ height: '100%', borderRight: 0 }}
                className="styMenu"
            >
                {this.state.leftMenu}
            </Menu>
        )
    }
}

export default LeftMenu
